﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class FindTaskListItem : FindLineTreeViewItem
    {
        public FindTaskListItem(string fileFullPath, string fileShortName, int lineNumber, int startLineOffset, int keywordLength,
                                string lineText, Brush foreColor, ItemType type, TextDecorationCollection textDecorations = null) :
                                                 base(fileFullPath, fileShortName, lineNumber, startLineOffset, keywordLength, lineText, foreColor, type, textDecorations)
        {
            this.MouseDown += FindTaskListItem_MouseDown;
        }

        private void FindTaskListItem_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            e.Handled = true;
            this.IsSelected = true;
        }

        /// <summary>
        /// 用于刷新StartDateTime和EndDateTime的值。
        /// </summary>
        public void UpdateDateTime()
        {
            DateTime? dtS = null;
            DateTime? dtE = null;

            foreach (var subitem in this.Items)
            {
                var item = subitem as FindTaskListTimeTagItem;
                if (item == null) continue;

                if (dtS == null || dtS.HasValue == false) dtS = item.TagDateTime;
                else
                {
                    if (dtS.Value.CompareTo(item.TagDateTime.Value) > 0)
                    {
                        dtS = item.TagDateTime;
                    }
                }

                if (dtE == null || dtE.HasValue == false) dtE = item.TagDateTime;
                else
                {
                    if (dtE.Value.CompareTo(item.TagDateTime.Value) < 0)
                    {
                        dtE = item.TagDateTime;
                    }
                }
            }

            if (dtS != null && dtS.HasValue) StartDateTime = dtS.Value;

            if (dtE != null && dtE.HasValue) EndDateTime = dtE.Value;
        }

        /// <summary>
        /// 判断提供的名称列表中有没有一个在当前项的名称列表中。
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        internal bool HasName(string[] names)
        {
            if (this.NamesList == null || this.NamesList.Count <= 0) return false;

            foreach (var lname in this.NamesList)
            {
                foreach (var name in names)
                {
                    if (lname.ToLower().IndexOf(name.Trim().ToLower()) >= 0) return true;
                }
            }

            return false;
        }

        internal string GetConfigValue(string configKey)
        {
            if (string.IsNullOrWhiteSpace(configKey)){ return null; }

            if (this.configDictionary == null || this.configDictionary.Count <= 0) return null;

            if(this.configDictionary.TryGetValue(configKey, out var value))
            {
                return value;
            }

            return null;
        }

        /// <summary>
        /// 不用管配置 Key 是什么内容；只管能不能匹配到对应的值。
        /// </summary>
        /// <param name="filterConfigValue"></param>
        /// <returns></returns>
        internal bool HasConfigValue(string filterConfigValue)
        {
            if (string.IsNullOrWhiteSpace(filterConfigValue)) filterConfigValue = "";

            if (this.ConfigDictionary == null) return false;

            foreach (var dic in this.ConfigDictionary)
            {
                if (string.IsNullOrWhiteSpace(dic.Value))
                {
                    if (filterConfigValue == "") return true;
                    else continue;
                }
                else
                {
                    if (filterConfigValue == "") return true;
                    else
                    {
                        if (dic.Value.ToLower().IndexOf(filterConfigValue.ToLower()) != -1)
                        {
                            return true;
                        }
                        else continue;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Begin time Or First time.
        /// 注意：在使用之前应调用UpdateDateTime()方法刷新其值。
        /// </summary>
        public DateTime StartDateTime { get; private set; }

        /// <summary>
        /// Finish time Or Abort time.
        /// 注意：在使用之前应调用UpdateDateTime()方法刷新其值。
        /// </summary>
        public DateTime EndDateTime { get; private set; }
    }
}
